package com.rising.face.ali.bishi.strarray;

/**
 * @Author: rising
 * @Description: 一个字符串数组中，各个字符串进行组合，组合后的字符串没有一个字符相等，则为一个解。
 * 求给定字符串数组中，所有满足的解中，最长解的长度。
 * 例如：arr={"au","uc","ed"}
 * 满足的解为："","au","uc","ed","aued","uced"
 * 所以最长解是"aued","uced"，输出4
 * @Date: create in 2021/2/27 22:19
 */
public class StrArrayMaxStr {

    public static void main(String[] args) {
        String[] arr = {"au","uc","ed"};
        int max = 0;
        max = findMaxStr(arr, 0, max);
        System.out.println(max);
    }

    public static int findMaxStr(String[] array, int index, int max) {
        if (array.length == index) {
            return max;
        }
        String currentStr = array[index];
        String temp = currentStr;
        for (int i = 0; i < array.length; i++) {
            //和当前字符串相等则跳过
            if (array[i].equals(currentStr)) {
                continue;
            }

            char[] chars = array[i].toCharArray();
            char[] currentStrChars = temp.toCharArray();
            //是否相等
            boolean flag = false;
            for (int j = 0; j < currentStrChars.length; j++) {
                if (flag) {
                    break;
                }
                for (int k = 0; k < chars.length; k++) {
                    if (currentStrChars[j] == chars[k]) {
                        flag = true;
                        break;
                    }
                }
            }
            //不相等则进行拼接
            if (!flag) {
                temp = temp + array[i];
            }
        }
        max = max >= temp.length() ? max : temp.length();

        return findMaxStr(array, ++index, max);
    }
}
